//
//  HomeTableViewCell.swift
//  MicroBlog
//
//  Created by Apple on 16/9/23.
//  Copyright © 2016年 Apple. All rights reserved.
//

import UIKit
//首页cell
//1.原创微博视图
//2.转发微博视图
//3.底部tooBar视图
//cell 子控件之间的内容间距
let HMHomeTableViewCellMargin: CGFloat = 10
//设置微博内容字体大小
let HMHomeTableViewCellFontSize: CGFloat = 14

class HomeTableViewCell: UITableViewCell {
    //记录当前toolBar的顶部约束
    var toolBarTopConstraint: Constraint?
    //处理网络请求后的模型数组
    var statusViewModel: HMStatusViewModel? {
        didSet {
            //给cell响应的子控件绑定数据
            originalView.statusViewModel = statusViewModel
            toolBarView.statusViewModel = statusViewModel
            //移除toolBar的顶部约束
            toolBarTopConstraint?.uninstall()
            //判断是否有转发微博
            if statusViewModel?.status?.retweeted_status != nil {
                retweetView.isHidden = false
                toolBarView.snp_updateConstraints(closure: { (make) in
                    toolBarTopConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
                })
                retweetView.statusViewModel = statusViewModel
            } else {
                //没有转发微博,更新约束
                retweetView.isHidden = true
                toolBarView.snp_updateConstraints(closure: { (make) in
                    toolBarTopConstraint = make.top.equalTo(originalView.snp_bottom).constraint
                })
            }
        }
    }
    
    //懒加载原创微博
    private lazy var originalView: HMStatusOriginalView = {
        let view = HMStatusOriginalView()
        return view
    }()
    //转发微博视图
    private lazy var retweetView: HMStatusRetweetView = HMStatusRetweetView()
    //底部toolBar视图
    private lazy var toolBarView: HMStatusToolBarView = HMStatusToolBarView()
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //添加子控件,设置约束
    private func setupUI() {
        contentView.backgroundColor = UIColor(white: 0.95, alpha: 1)
        contentView.addSubview(originalView)
        contentView.addSubview(toolBarView)
        contentView.addSubview(retweetView)
        
        originalView.snp_makeConstraints { (make) in
            make.top.equalTo(contentView).offset(8)
            make.leading.trailing.equalTo(contentView)
        }
        
        retweetView.snp_makeConstraints { (make) in
            make.top.equalTo(originalView.snp_bottom)
            make.leading.trailing.equalTo(originalView)
            
        }
        
        toolBarView.snp_makeConstraints { (make) in
           toolBarTopConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
            make.leading.trailing.equalTo(retweetView)
//            make.height.equalTo(35)
            //关键约束
            make.bottom.equalTo(contentView.snp_bottom)
        }
        //设置向上的约束
        //swift2.0可以这样设置,但是3.0改变了
        //        contentView.snp_makeConstraints { (make) in
        //            make.bottom.equalTo(tooBarView)
        //            make.top.left.right.equalTo(self)
        //        }
        
    }
}
